package com.bey.bow.web.content;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bey.bow.entity.ContentDetail;
import com.bey.bow.service.content.ContentDetailService;
import com.bey.bow.utils.FileNameGenerator;

@Controller
@RequestMapping(value = "admin/contentDetail")
public class ContentDetailAdminController {

	private ContentDetailService contentDetailService;

	private static final int PAGE_SIZE = 10;

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("contentDetail", new ContentDetail());
		model.addAttribute("action", "create");
		return "admin/contentDetail/contentDetailForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(ContentDetail contentDetail,
			@RequestParam("file") MultipartFile file,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {

		if (file.isEmpty()) {
			return "/admin/course/course_add_fail";
		}

		String path = request.getSession().getServletContext()
				.getRealPath("static/upload/");
		String fileName = FileNameGenerator.getNewFileName(file);
		String filePath = path + "/" + fileName;
		contentDetail.setContentDetailUrl(fileName);
		File dest = new File(filePath);

		try {
			file.transferTo(dest);
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return "/admin/course/course_add_fail";
		} catch (IOException e) {
			e.printStackTrace();
			return "/admin/course/course_add_fail";
		}

		contentDetailService.saveContentDetail(contentDetail);
		redirectAttributes.addFlashAttribute("message", "创建任务成功");
		return "redirect:/admin/contentDetail/";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("contentDetail", contentDetailService.getContentDetail(id));
		model.addAttribute("action", "update");
		return "admin/contentDetail/contentDetailForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@ModelAttribute("contentDetail") ContentDetail contentDetail,
			RedirectAttributes redirectAttributes) {

		contentDetailService.saveContentDetail(contentDetail);
		redirectAttributes.addFlashAttribute("message", "更新任务成功");
		return "redirect:/admin/contentDetail/";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		contentDetailService.deleteContentDetail(id);
		redirectAttributes.addFlashAttribute("message", "删除课程成功");
		return "redirect:/admin/contentDetail/";
	}

	@RequestMapping(value = "")
	public String list(
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			Model model, ServletRequest request) {

		Page<ContentDetail> contentDetails = contentDetailService.getContentDetails(pageNumber,
				PAGE_SIZE, "auto");

		model.addAttribute("contentDetails", contentDetails);

		return "admin/contentDetail/contentDetailList";
	}

	@Autowired
	public void setContentDetailService(ContentDetailService contentDetailService) {
		this.contentDetailService = contentDetailService;
	}

}
